import axios from 'axios';
import { Message } from 'element-ui';

axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'

const request = axios.create({
  baseURL: process.env.NODE_ENV === "production" ? "https://api.javaxmsz.com/" : '/api',
  timeout: 10000,
});

// 添加响应拦截器
request.interceptors.response.use(function (response) {
  if (response.data.success) {
    return response.data;
  } else if (response.data.code === 401) {
    window.location.href = window.location.origin + '/login';
  } else {
    Message.error(response.data.message || '服务异常！');
    return Promise.reject(response);
  }
}, function (error) {
  Message.error(error.message || '服务异常！');
  return Promise.reject(error);
});

export function setDefaultToken (token) {
  request.defaults.headers['Authorization'] = token;
}

// 登录
export function login(data) {
  return request({
    url: `sso/login?username=${data.username}&password=${data.password}`,
    method: 'post',
  });
}

// 查询商品详情
export function getGoodDetail (id) {
  return request({
    url: `/product/detail/${id}`,
    method: 'get',
  })
}

// 商品搜索
export function goodSearch (params) {
  return request({
    url: '/product/search',
    method: 'get',
    params,
  })
}

// 添加商品到购物车
export function addProduct (data) {
  return request({
    url: 'cart/add',
    method: 'post',
    data,
  });
}

// 查询购物车列表
export function getCartList () {
  return request({
    url: 'cart/list',
    method: 'get',
  });
}

// 删除购物车
export function deleteCartGood (ids) {
  return request({
    url: 'cart/delete?ids=' + ids,
    method: 'post',
  });
}

// 修改购物车中的商品数量
export function editCartGoodCount (params) { 
  return request({
    url: '/cart/update/quantity',
    method: 'get',
    params,
  });
}

export function clearGoodCart () {
  return request({
    url: '/cart/clear',
    method: 'post',
  });
}

// 查询购物车中某个商品的详情
export function getOrderDetail (id) {
  return request({
    url: '/cart/getInfo/' + id,
    method: 'get',
  });
}

// 查询购物车中某个商品的详情
export function calcuPrice (data) {
  return request({
    url: '/cart/calcuPrice',
    method: 'post',
    data
  });
}

// 提交订单
export function submitOrder (data) {
  return request({
    url: '/order/generateOrder',
    method: 'post',
    data,
  });
}

// 查询客户列表
export function getCustomerList () {
  return request({
    url: '/ext/customer/listAll',
    method: 'get',
  });
}

// 查询计算器下拉字典
export function getSelectOptions (params) {
  return request({
    url: '/ext/product/dict/listByProductCategoryId',
    method: 'get',
    params
  });
}




// 获取分类
export function getCategoryList () {
  return request({
    url: '/sh/category',
    method: 'get',
  });
}

// 查询首页轮播图
export function getHomeImgList () {
  return request({
    url: '/sh/ad/type/LB',
    method: 'get',
  });
}

// 查询2号广告位
export function getAdByType (type) {
  return request({
    url: `/sh/ad/type/${type}`,
    method: 'get',
  });
}

// 查询博客
export function getBlog (params) {
  return request({
    url: '/sh/blog',
    method: 'get',
    params
  });
}

// 查询标签
export function getLabel (category) {
  return request({
    url: '/sh/tag',
    method: 'get',
    params: {
      page: 1,
      limit: 10000,
      category
    }
  });
}

// 查询博客详情
export function getBlogDetailById (id) {
  return request({
    url: `/sh/blog/${id}`,
    method: 'get',
  });
}




